﻿using System;
using System.Configuration;
using System.Reflection;
using CY_MVC.Core;
using CY_MVC.UrlRewrite;

namespace CY_MVC
{
    public static class StaticConfig
    {
        #region MVC设置属性

        public static string MVCHandler
        {
            get { return "/CYMVC.axd"; }
        }

        /// <summary>
        /// 后台DLL名称（必填）
        /// </summary>
        public static string ControllerName
        {
            get;
            internal set;
        }

        /// <summary>
        /// 模版路径（必填）
        /// </summary>
        public static string TemplatePath
        {
            get;
            internal set;
        }

        /// <summary>
        /// 程序根路径
        /// </summary>
        public static string RootPath
        {
            get;
            internal set;
        }

        /// <summary>
        /// 是否开启CSS合并
        /// </summary>
        public static bool CssMerge
        {
            get;
            internal set;
        }

        /// <summary>
        /// 是否开启JS合并  开启以后不想合并的调用可以>;</script>即可忽略合并
        /// </summary>
        public static bool JsMerge
        {
            get;
            internal set;
        }

        /// <summary>
        /// 在开启JS或者CSS合并的情况下,服务器和客户端缓存时间
        /// </summary>
        public static int JsCssCacheHour
        {
            get;
            internal set;
        }

        /// <summary>
        /// GZIP压缩最低大小，单位kb（推荐数值200）
        /// </summary>
        public static int GzipLength
        {
            get;
            internal set;
        }

        /// <summary>
        /// URL重载规则文件路径
        /// </summary>
        public static string UrlRemapRulesPath
        {
            get;
            internal set;
        }

        /// <summary>
        /// URL重写规则文件路径
        /// </summary>
        public static string UrlRewriteRulesPath
        {
            get;
            internal set;
        }

        /// <summary>
        /// URL重写支持文件后缀(多个后缀请用|分开)
        /// </summary>
        public static string UrlRewriteExtensions
        {
            get;
            internal set;
        }

        /// <summary>
        /// 关键词过滤规则文件路径
        /// </summary>
        public static string WordFilterRulesPath
        {
            get;
            internal set;
        }

        /// <summary>
        /// 模版引擎DLL名称
        /// </summary>
        public static string ViewTemplateName
        {
            get;
            internal set;
        }


        /// <summary>
        /// 防攻击 时间范围内访问次数限制
        /// </summary>
        public static int DDosAttackBannedCount
        {
            get;
            internal set;
        }

        /// <summary>
        /// 防攻击 多少秒判断一次
        /// </summary>
        public static int DDosAttackCheckPerSeconds
        {
            get;
            internal set;
        }

        /// <summary>
        /// 防攻击 判断成立被禁止访问的秒数
        /// </summary>
        public static int DDosAttackBennedSeconds
        {
            get;
            internal set;
        }

        #endregion

        static StaticConfig()
        {
            var Config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().CodeBase.Substring(8));
            RootPath = (Config.AppSettings.Settings["RootPath"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["RootPath"].Value)) ? AppDomain.CurrentDomain.BaseDirectory.Replace("\\","/") : (AppDomain.CurrentDomain.BaseDirectory + Config.AppSettings.Settings["RootPath"].Value.TrimStart('/').TrimStart('\\')).Replace("\\", "/");
            TemplatePath = (Config.AppSettings.Settings["TemplatePath"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["TemplatePath"].Value)) ? "" : Config.AppSettings.Settings["TemplatePath"].Value.Replace("\\", "/");
            ControllerName = (Config.AppSettings.Settings["ControllerName"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["ControllerName"].Value)) ? "" : Config.AppSettings.Settings["ControllerName"].Value;
            CssMerge = (Config.AppSettings.Settings["CssMerge"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["CssMerge"].Value)) ? false : bool.Parse(Config.AppSettings.Settings["CssMerge"].Value);
            JsMerge = (Config.AppSettings.Settings["JsMerge"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["JsMerge"].Value)) ? false : bool.Parse(Config.AppSettings.Settings["JsMerge"].Value);
            JsCssCacheHour = (Config.AppSettings.Settings["JsCssCacheHour"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["JsCssCacheHour"].Value)) ? 6 : int.Parse(Config.AppSettings.Settings["JsCssCacheHour"].Value);
            GzipLength = (Config.AppSettings.Settings["GzipLength"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["GzipLength"].Value)) ? 0 : int.Parse(Config.AppSettings.Settings["GzipLength"].Value) * 1024;
            UrlRewriteRulesPath = (Config.AppSettings.Settings["UrlRewriteRulesPath"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["UrlRewriteRulesPath"].Value)) ? string.Empty : RootPath + Config.AppSettings.Settings["UrlRewriteRulesPath"].Value.TrimStart('/').TrimStart('\\').Replace("\\", "/");
            UrlRewriteExtensions = (Config.AppSettings.Settings["UrlRewriteExtensions"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["UrlRewriteExtensions"].Value)) ? "html" : Config.AppSettings.Settings["UrlRewriteExtensions"].Value;
            UrlRemapRulesPath = (Config.AppSettings.Settings["UrlRemapRulesPath"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["UrlRemapRulesPath"].Value)) ? string.Empty : RootPath + Config.AppSettings.Settings["UrlRemapRulesPath"].Value.TrimStart('/').TrimStart('\\').Replace("\\", "/");
            WordFilterRulesPath = (Config.AppSettings.Settings["WordFilterRulesPath"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["WordFilterRulesPath"].Value)) ? string.Empty : RootPath + Config.AppSettings.Settings["WordFilterRulesPath"].Value.TrimStart('/').TrimStart('\\').Replace("\\", "/");
            DDosAttackBannedCount = (Config.AppSettings.Settings["DDosAttackBannedCount"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["DDosAttackBannedCount"].Value)) ? 0 : int.Parse(Config.AppSettings.Settings["DDosAttackBannedCount"].Value);
            DDosAttackCheckPerSeconds = (Config.AppSettings.Settings["DDosAttackCheckPerSeconds"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["DDosAttackCheckPerSeconds"].Value)) ? 0 : int.Parse(Config.AppSettings.Settings["DDosAttackCheckPerSeconds"].Value);
            DDosAttackBennedSeconds = (Config.AppSettings.Settings["DDosAttackBennedSeconds"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["DDosAttackBennedSeconds"].Value)) ? 0 : int.Parse(Config.AppSettings.Settings["DDosAttackBennedSeconds"].Value);
            ViewTemplateName = (Config.AppSettings.Settings["ViewTemplateName"] == null || string.IsNullOrWhiteSpace(Config.AppSettings.Settings["ViewTemplateName"].Value)) ? "" : Config.AppSettings.Settings["ViewTemplateName"].Value;

            if (string.IsNullOrWhiteSpace(TemplatePath) || string.IsNullOrWhiteSpace(ControllerName))
            {
                throw new Exception("必须设置模版路径和后台程序DLL文件名称！");
            }

            DDosAttackHelper.Init();
            ControllerContext.Init();
            AjaxMethodContext.Init();
            UrlRewriteHelper.Init();
            UrlRemapHelper.Init();
            WordFilterHelper.Init();
        }
    }
}
